package net.expectx.gw.controller;

import com.baidu.unbiz.fluentvalidator.FluentValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.gw.bean.ThirdSmsCode;
import net.expectx.gw.service.ThirdSmsCodeService;
import net.expectx.gw.util.AdminConstants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller
 * @author lijian
 * @date 2019/1/18
 */
@Controller
@RequestMapping("/manager/sms")
@Api(value = "控制器", description = "管理")
public class ThirdSmsCodeController extends BaseController {
    @Autowired
    private ThirdSmsCodeService thirdSmsCodeService;

    private static final Logger LOGGER = LoggerFactory.getLogger(ThirdSmsCodeController.class);
    @ApiOperation(value = "获得验证码")
    @GetMapping("/verification_code")
    @ResponseBody
    public Object getVerificationCode(
            String codeDevice,
            Byte codeType){
        // 参数检查
        FluentValidator fluentValidator = FluentValidator.checkAll();
        Map map=thirdSmsCodeService.getVerificationCode(codeDevice,codeType,AdminConstants.CODE_EXPIRY_TIME);
        return map;
    }

    @ApiOperation(value = "校验验证码")
    @GetMapping("/verification_verification_code")
    @ResponseBody
    public Object  verificationVerificationCode(String codeOrder,String code){
        // 参数检查
        Map map=thirdSmsCodeService.verificationVerificationCode(codeOrder,code);
        return map;
    }


    @ApiOperation(value = "验证码首页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {

        return "third/smsCode/index";
    }

    @ApiOperation(value = "列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit
            ,String codeDevice) {
        String strWhere="1=1 and is_deleted=0 ";
        String strOrder=null;
        if (StringUtils.isNotBlank(codeDevice)) {
            strWhere+=" and code_device like('%" + codeDevice + "%')";
        }
        List<ThirdSmsCode> thirdSmsCodes = thirdSmsCodeService.findByPage(page,limit,strWhere,strOrder);
        long total = thirdSmsCodeService.count(strWhere);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", thirdSmsCodes);
        return result;
    }

    @ApiOperation(value = "删除验证码")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        Map<String, Object> map = new HashMap<>(2);
        int count = thirdSmsCodeService.logicDeletion(AdminConstants.YES, "id in(" + ids + ")");
        if (count > 0) {
            count = thirdSmsCodeService.logicDeletion(AdminConstants.YES, "id in(" + ids + ")");
            map.put("flag", count > 0 ? true : false);
        } else {
            map.put("flag", count > 0 ? true : false);

        }

        return map;
    }
}
